package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodAdapter extends BaseAdapter {
    Context context;
    List<FoodBean.DataBean> data;

    public FoodAdapter(Context context, List<FoodBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh = null;
        if (convertView ==null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_food, null);
            vh = new VH(convertView);
            convertView.setTag(vh);

        }else {
            vh = (VH) convertView.getTag();
        }
        FoodBean.DataBean fb = data.get(position);
        vh.tv1.setText(fb.getTitle());;
        vh.tv2.setText("配料:"+fb.getFood_str());
        Picasso.with(context).load(fb.getPic()).into(vh.iv);


        return convertView;
    }
    class VH {
        ImageView iv;
        TextView tv1,tv2;
        public VH(View view) {
            iv = view.findViewById(R.id.item_iv);
            tv1 = view.findViewById(R.id.item_tv1);
            tv2 = view.findViewById(R.id.item_tv2);
        }
    }
}
